#!/bin/bash
echo "开始安装 jdk-1.8"
JDK_INSTALL_PATH="/data/soft/java"
JDK_TAR_FILE="jdk-8u51-linux-x64.tar.gz"
JDK_FILE="jdk1.8.0_51"
ENV_DIR="/data/env"
ENV_FILE="$ENV_DIR/environment"
if [ ! -d "$ENV_DIR" ];then
	echo "环境文件目录不存在，创建目录[$ENV_DIR]"
	mkdir -p $ENV_DIR
fi


if [ ! -d "$JDK_INSTALL_PATH" ] ; then
	echo "安装目录不存在，创建安装目录[$JDK_INSTALL_PATH]"
	mkdir -p $JDK_INSTALL_PATH
fi
if [ ! -f "$JDK_TAR_FILE" ] ; then
	echo "JDK安装包不存在，开始下载安装包"
	wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.
oracle.com/otn-pub/java/jdk/8u51-b16/jdk-8u51-linux-x64.tar.gz
	echo "JDK安装包下载完成"
	cp ./$JDK_TAR_FILE $JDK_INSTALL_PATH
fi
cd $JDK_INSTALL_PATH
if [ -d "$JDK_INSTALL_PATH/$JDK_FILE" ]; then
	echo "删除已存在目录[$JDK_INSTALL_PATH/$JDK_FILE],重新解压缩"
	rm -rf $JDK_INSTALL_PATH/$JDK_FILE
fi
tar -xvf $JDK_TAR_FILE
echo "解压缩安装包完成。。。"

if [ ! -f "$ENV_FILE" ]; then
	echo "环境变量文件不存在，创建文件[$ENV_FILE]"
	touch $ENV_FILE
fi

echo "配置环境变量开始"
if  ! grep "JAVA_HOME=" /data/env/environment
then
	echo "JAVA_HOME=$JDK_INSTALL_PATH/$JDK_FILE" | tee -a $ENV_FILE
	echo "export JAVA_HOME" | tee -a $ENV_FILE
	echo 'PATH=$PATH:$JAVA_HOME/bin' | tee -a $ENV_FILE
	echo "export PATH" | tee -a $ENV_FILE
	echo 'CLASSPATH=.:$JAVA_HOME/lib' | tee -a $ENV_FILE
	echo "export CLASSPATH" | tee -a $ENV_FILE
fi


echo "JDK安装完成"